﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess.Advice
{
    public sealed class ResultAdvice:BaseAdvice
    {
        private ChessBoard _chessBoard;
        private Player _player;
        private ChessPiece _chessPiece;
        private Dir _dir;
        public ResultAdvice(ChessBoard chessBoard, Player player, ChessPiece chessPiece, Dir dir) {
            this._chessBoard = chessBoard;
            this._player = player;
            this._chessPiece = chessPiece;
            this._dir = dir;
        }

        public override int Execute()
        {
            AdviceStatusEnum status = AdviceStatusEnum.None;
            //棋子下一步棋位
            var pos = _chessBoard.GetChessPieceAround(_chessPiece, _dir);
            if (pos != null && !_chessBoard.IsEmpty(pos.Value)) {
                //棋子吃子了，则再下一步棋位
                pos = _chessBoard.GetChessPieceAround(pos.Value,_dir);                
            }

            if (pos != null) 
            {
                if (Dir.TOP != NegativeDir(_dir)) {
                    var pos1 = _chessBoard.GetChessPieceAround(pos.Value,Dir.TOP);
                    if (pos1 == null || _chessBoard.IsEmpty(pos1.Value) || _player.IsMyChessPiece(pos1.Value))
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_安全);                     
                    }
                    else {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_被吃);
                    }
                }
                if (Dir.BOTTOM != NegativeDir(_dir))
                {
                    var pos1 = _chessBoard.GetChessPieceAround(pos.Value, Dir.BOTTOM);
                    if (pos1 == null || _chessBoard.IsEmpty(pos1.Value) || _player.IsMyChessPiece(pos1.Value))
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_安全);
                    }
                    else
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_被吃);
                    }
                }
                if (Dir.LEFT != NegativeDir(_dir))
                {
                    var pos1 = _chessBoard.GetChessPieceAround(pos.Value, Dir.LEFT);
                    if (pos1 == null || _chessBoard.IsEmpty(pos1.Value) || _player.IsMyChessPiece(pos1.Value))
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_安全);
                    }
                    else
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_被吃);
                    }
                }
                if (Dir.RIGHT != NegativeDir(_dir))
                {
                    var pos1 = _chessBoard.GetChessPieceAround(pos.Value, Dir.RIGHT);
                    if (pos1 == null || _chessBoard.IsEmpty(pos1.Value) || _player.IsMyChessPiece(pos1.Value))
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_安全);
                    }
                    else
                    {
                        status = CompareAdviceStatus(status, AdviceStatusEnum.落子_被吃);
                    }
                }
            }

            _score += GetScore(status); ;
            return base.Execute();
        }

    }
}
